package bamin.com.kepiao.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.aiton.administrator.shane_library.shane.utils.HTTPUtils;
import com.aiton.administrator.shane_library.shane.utils.VersionUtils;
import com.aiton.administrator.shane_library.shane.utils.VolleyListener;
import com.aiton.administrator.shane_library.shane.widget.SingleBtnDialog;
import com.alipay.sdk.app.PayTask;
import com.android.volley.VolleyError;
import com.switfpass.pay.MainApplication;
import com.switfpass.pay.activity.PayPlugin;
import com.switfpass.pay.bean.RequestMsg;
import com.umeng.analytics.MobclickAgent;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

import bamin.com.kepiao.R;
import bamin.com.kepiao.Zalipay.PayResult;
import bamin.com.kepiao.constant.Constant;
import bamin.com.kepiao.models.Sign;
import bamin.com.kepiao.models.about_order.QueryOrder;
import bamin.com.kepiao.models.about_redpacket.RedPacket;
import bamin.com.kepiao.models.about_xyblank_pay.XingYeBlankPayInfo;
import bamin.com.kepiao.utils.GetIpAddressUtil;
import bamin.com.kepiao.utils.TimeAndDateFormate;

/**
 * 优化1.0
 */
public class PayActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = "PayActivity";
    private Handler mHandlerTime = new Handler();
    private double realPayPrice;//真正支付的金额
    private int lastTime = 600;//剩余支付时间
    private TextView mTicket_count;
    private String mOrderState;
    private TextView mSurplusTime;
    private String mBookLogAID;
    private QueryOrder mQueryOrder;
    private RadioGroup mPay_mode;
    private RadioButton mRadioButton_zhifubao;
    private RadioButton mRadioButton_weixin;
    private String payMode = "微信支付";//支付方式
    private RelativeLayout mRela_useTheRedBag;
    private TextView mTextView_redBagCount;
    private RedPacket mRedBag;
    private TextView mTicket_price;
    private double mPrice;
    private String mOrderId;
    private Map<String, String> mGetWechatOrderParams;
    private Runnable mR;
    private String mId;
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_CHECK_FLAG = 2;
    private String mOutTradeNo;
    private String mIsSure;
    private boolean isEnterWX = false;
    public static final String PAY_RECEIVER = "wxpay";
    /**
     * 接收红包界面发来的广播
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "RedBag":
                    mRedBag = (RedPacket) intent.getSerializableExtra("RedBag");
                    double amount = mRedBag.getAmount();
                    realPayPrice = mPrice - amount;
                    if (realPayPrice < 0) {
                        realPayPrice = 0;
                    }
                    mTicket_price.setText("¥" + realPayPrice + "=" + mPrice + "-" + amount);
                    break;
                case PAY_RECEIVER:
                    int error = intent.getIntExtra("error", 0);
                    switch (error) {
                        case 0:
                            showChargeResultSuccess("支付成功", "查看订单");
                            break;
                        case -1:
                            showChargeResult("支付失败", "确认");
                            break;
                        case -2:
                            showChargeResult("取消支付", "确认");
                            break;
                        default:
                            break;
                    }
                    break;
            }
        }
    };

    /**
     * 支付宝支付动作完成后的回调
     */
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    PayResult payResult = new PayResult((String) msg.obj);
                    /**
                     * 同步返回的结果必须放置到服务端进行验证（验证的规则请看https://doc.open.alipay.com/doc2/
                     * detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665&
                     * docType=1) 建议商户依赖异步通知
                     */
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
                    if (TextUtils.equals(resultStatus, "9000")) {
                        //支付成功向金点通发送确认订单
                        showChargeResultSuccess("支付成功", "查看订单");
                    } else {
                        // 判断resultStatus 为非"9000"则代表可能支付失败
                        // "8000"代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
                        if (TextUtils.equals(resultStatus, "8000")) {
                            showChargeResult("支付结果确认中", "确认");
                        } else {
                            // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
//                            cancleOrder();
                            showChargeResult("支付失败", "确认");
                        }
                    }
                    break;
                }
                case SDK_CHECK_FLAG: {
                    Toast.makeText(PayActivity.this, "检查结果为：" + msg.obj, Toast.LENGTH_SHORT).show();
                    break;
                }
                default:
                    break;
            }
        }
    };
    private TextView mTextView_ticketPrice;
    private TextView mTextView_insurePrice;
    private double mInsurePrice;
    private RadioButton mRadioButton_yinlian;
    private TextView mTextView_isHasChild;
    private TextView mTextView_gaosu;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        //获取用户id
        SharedPreferences sp = getSharedPreferences(Constant.SP_KEY.SP_NAME, Context.MODE_PRIVATE);
        mId = sp.getString(Constant.SP_KEY.ID, "");
        initIntent();
        findID();
        queryOrderInfo();
        queryRedBag();
        queryTime(false);
        setListener();
    }

    /**
     * 查询优惠券信息
     */
    private void queryRedBag() {
        String url = Constant.HOST_TICKET + "/redenvelope/getnumofredenvelopebyuser";
        Map<String, String> map = new HashMap<>();
        map.put("account_id", mId);
        HTTPUtils.post(PayActivity.this, url, map, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }

            @Override
            public void onResponse(String s) {
                mTextView_redBagCount.setText(s + "张");
            }
        });
    }

    private void findID() {
        mTicket_price = (TextView) findViewById(R.id.ticket_price);
        mPay_mode = (RadioGroup) findViewById(R.id.pay_mode);
        mRadioButton_zhifubao = (RadioButton) findViewById(R.id.radioButton_zhifubao);
        mRadioButton_weixin = (RadioButton) findViewById(R.id.radioButton_weixin);
        mRadioButton_yinlian = (RadioButton) findViewById(R.id.radioButton_yinlian);
        mRela_useTheRedBag = (RelativeLayout) findViewById(R.id.rela_useTheRedBag);
        mTextView_redBagCount = (TextView) findViewById(R.id.textView_redBagCount);
        mTicket_count = (TextView) findViewById(R.id.ticket_count);
        mSurplusTime = (TextView) findViewById(R.id.surplusTime);
        mTextView_ticketPrice = (TextView) findViewById(R.id.textView_ticketPrice);
        mTextView_insurePrice = (TextView) findViewById(R.id.textView_insurePrice);
        mTextView_isHasChild = (TextView) findViewById(R.id.textView_isHasChild);
        mTextView_gaosu = (TextView) findViewById(R.id.textView_gaosu);
    }

    /**
     * 查询订单信息
     */
    private void queryOrderInfo() {
        String url = Constant.JDT_TICKET_HOST + "QueryBookLog?getTicketCodeOrAID=" + mBookLogAID;
        HTTPUtils.get(PayActivity.this, url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }

            @Override
            public void onResponse(String s) {
                Document doc = null;
                try {
                    doc = DocumentHelper.parseText(s);
                    Element testElement = doc.getRootElement();
                    String testxml = testElement.asXML();
                    String result = testxml.substring(testxml.indexOf(">") + 1, testxml.lastIndexOf("<"));
                    mQueryOrder = GsonUtils.parseJSON(result, QueryOrder.class);
                    initUI();
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 查询并更新时间
     */
    private void queryTime(final boolean isPay) {
        if (isPay) {
            mProgressDialog = new ProgressDialog(PayActivity.this);
            mProgressDialog.setMessage("生成订单中……");
            mProgressDialog.setCancelable(false);
            mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                        HTTPUtils.cancelAll(PayActivity.this);
                        mProgressDialog.dismiss();
                    }
                    return false;
                }
            });
            mProgressDialog.show();
        }
        String url_web = Constant.JDT_TICKET_HOST +
                "SellTicket_Other_NoBill_GetBookStateAndMinuteToConfirm?scheduleCompanyCode=" + "YongAn" + "" +
                "&bookLogID=" + mBookLogAID;
        Log.e("queryTime ", "queryTime 剩余时间"+mBookLogAID);
        HTTPUtils.get(PayActivity.this, url_web, new VolleyListener() {
            public void onErrorResponse(VolleyError volleyError) {
                mProgressDialog.dismiss();
            }

            public void onResponse(String s) {
                Document doc = null;
                try {
                    doc = DocumentHelper.parseText(s);
                    Element testElement = doc.getRootElement();
                    String testxml = testElement.asXML();
                    Log.e("onResponse ", "onResponse 剩余时间"+testxml);
                    mOrderState = testxml.substring(testxml.indexOf(">") + 1, testxml.lastIndexOf("<"));
                    String[] split = mOrderState.split(",");
                    if (split.length>2){
                        lastTime = (int) (Double.parseDouble(split[2])*60 - Constant.YULIUZHIFU_TIME);
                    }else{
                        lastTime=0;
                    }
                    if (isPay){
                        payFor();
                    }else {
                        setTime();
                    }
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 票订单支付倒计时
     */
    private void setTime() {

        mR = new Runnable() {
            @Override
            public void run() {
                mSurplusTime.setText(TimeFormat(lastTime));
                if (lastTime == 0) {
                    return;
                }
                lastTime--;
                mHandlerTime.postDelayed(mR, 1000);
            }
        };
        mHandlerTime.postDelayed(mR, 0);
    }

    //将秒数转换成时间格式
    private String TimeFormat(int progress) {
        int min = progress / 60;
        int sec = progress % 60;
        //设置整数的输出格式：  %02d  d代表int  2代码位数    0代表位数不够时前面补0
        String time = String.format("%02d", min) + ":" + String.format("%02d", sec);
        return time;
    }

    private void initIntent() {
        Intent intent = getIntent();
        mBookLogAID = intent.getStringExtra("BookLogAID");
        Log.e("initIntent ", "initIntent mBookLogAID" + mBookLogAID);
        mOrderId = intent.getStringExtra("OrderId");
        Log.e("initIntent ", "initIntent PayActivity OrderId" + mOrderId);
        mPrice = intent.getDoubleExtra("realPrice", 0);
        mIsSure = intent.getStringExtra("isSure");
        mInsurePrice = intent.getDoubleExtra("insurePrice", 0);
    }

    private void setListener() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.pay).setOnClickListener(this);
        mPay_mode.setOnCheckedChangeListener(new MyOnCheckChangeListener());
        mRela_useTheRedBag.setOnClickListener(this);
        findViewById(R.id.textView_order_cancle).setOnClickListener(this);
    }

    class MyOnCheckChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (mRadioButton_zhifubao.isChecked()) {
                Log.e("onCheckedChanged", "支付宝");
                payMode = "支付宝";
            } else if (mRadioButton_weixin.isChecked()) {
                payMode = "微信支付";
            } else if (mRadioButton_yinlian.isChecked()) {
                payMode = "银联支付";
            }
        }
    }

    private void initUI() {
        mTicket_count.setText(mQueryOrder.getFullTicket() + "");
        realPayPrice = mPrice;
        mTicket_price.setText("¥" + realPayPrice);
        TextView textView_detialOrderDate = (TextView) findViewById(R.id.textView_detialOrderDate);
        textView_detialOrderDate.setText("出发日期" + TimeAndDateFormate.dateFormate(mQueryOrder.getSetoutTime()));
        TextView textView_detialOrderStartPlace = (TextView) findViewById(R.id.textView_detialOrderStartPlace);
        textView_detialOrderStartPlace.setText(mQueryOrder.getStartSiteName());
        TextView textView_detialOrderEndPlace = (TextView) findViewById(R.id.textView_detialOrderEndPlace);
        textView_detialOrderEndPlace.setText(mQueryOrder.getEndSiteName());
        TextView textView_detialOrderTime = (TextView) findViewById(R.id.textView_detialOrderTime);
        textView_detialOrderTime.setText(TimeAndDateFormate.timeFormate(mQueryOrder.getSetoutTime()));
        mTextView_ticketPrice.setText("¥" + mQueryOrder.getPrice());
        mTextView_insurePrice.setText("¥" + mInsurePrice + "×" + mQueryOrder.getFullTicket());
        if (mQueryOrder.isCarryChild()) {
            mTextView_isHasChild.setText("是");
        }else {
            mTextView_isHasChild.setText("否");
        }
        //高速非高速
        String lineName = mQueryOrder.getLineName();
        boolean buFenGaoSu = lineName.contains("部分高速");
        if (buFenGaoSu){

        }else{
            boolean gaoSu = lineName.contains("高速");
            if (gaoSu) {
                mTextView_gaosu.setVisibility(View.VISIBLE);
            }else {
                mTextView_gaosu.setVisibility(View.GONE);
            }
        }
    }

    /*-----------------------支付宝Start-----------------------*/

    private void getSign() {
        String url = Constant.Url.GETSIGN;
        Map<String, String> map = new HashMap<>();
//        map.put("out_trade_no", mQueryOrder.getAID().substring(0,32));
        map.put("subject", mQueryOrder.getStartSiteName() + "-" + mQueryOrder.getEndSiteName()+" "+ VersionUtils.getCurrVersionInfo(PayActivity.this));
        map.put("total_fee", realPayPrice + "");
        map.put("id", mOrderId);
        if (mRedBag != null) {
            map.put("redEnvelope_id", mRedBag.getId() + "");
        }
        map.put("real_pay", realPayPrice + "");
        Log.e("getSign ", "getSign "+ url + "?out_trade_no="+getOutTradeNo()+"&subject="+mQueryOrder.getStartSiteName() + "-" + mQueryOrder.getEndSiteName()
                +"&total_fee="+realPayPrice
        +"&id="+mOrderId+"&real_pay="+realPayPrice);
        HTTPUtils.post(PayActivity.this, url, map, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressDialog.dismiss();
            }

            @Override
            public void onResponse(String s) {
                Log.e("PayActivity", "PayActivity--onResponse--支付宝获取签名"+s);
                mProgressDialog.dismiss();
                Sign sign = GsonUtils.parseJSON(s, Sign.class);
                final String body = sign.getBody();
                Log.e("onResponse", "返回值" + body);
                if (!TextUtils.isEmpty(body)) {
                    Runnable payRunnable = new Runnable() {

                        @Override
                        public void run() {
                            // 构造PayTask 对象
                            PayTask alipay = new PayTask(PayActivity.this);
                            // 调用支付接口，获取支付结果
                            String result = alipay.pay(body, true);

                            Message msg = new Message();
                            msg.what = SDK_PAY_FLAG;
                            msg.obj = result;
                            mHandler.sendMessage(msg);
                        }
                    };

                    // 必须异步调用
                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                }else{
                    showChargeResult("获取签名失败","确认");
                }
            }
        });
    }


    /**
     * get the out_trade_no for an order. 生成商户订单号，该值在商户端应保持唯一（可自定义格式规范）
     */
    private String getOutTradeNo() {
        SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
        Date date = new Date();
        String key = format.format(date);

        Random r = new Random();
        key = key + r.nextInt();
        key = key.substring(0, 15);
        return key;
    }

    //支付失败dialog提示
    private void setFailDialog01(String messageTxt, String iSeeTxt) {
        View commit_dialog = getLayoutInflater().inflate(R.layout.commit_dialog, null);
        TextView message = (TextView) commit_dialog.findViewById(R.id.message);
        Button ISee = (Button) commit_dialog.findViewById(R.id.ISee);
        message.setText(messageTxt);
        ISee.setText(iSeeTxt);
        AlertDialog.Builder builder = new AlertDialog.Builder(PayActivity.this);
        final AlertDialog dialog = builder.setView(commit_dialog).create();
        dialog.setCancelable(false);
        dialog.show();
        commit_dialog.findViewById(R.id.ISee).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if ("isSure".equals(mIsSure)) {
                    finish();
                } else {
                    startToMainActivity();
                }
                animFromBigToSmallOUT();
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.textView_order_cancle:
                setDialog("确认取消订单吗？");
                break;
            case R.id.rela_useTheRedBag:
                intent.setClass(PayActivity.this, CouponInfoActivity.class);
                intent.putExtra("UseRedBag", "UseRedBag");
                startActivity(intent);
                animFromLeftToRightIN();
                break;
            case R.id.pay:
                queryTime(true);
                break;
            case R.id.iv_back:
                if ("isSure".equals(mIsSure)) {
                    finish();
                } else {
                    startToMainActivity();
                }
                animFromBigToSmallOUT();
                break;
        }
    }

    private void payFor() {
        if (lastTime > 0) {
            if (realPayPrice == 0) {
                //用了优惠券减到0的情况
//                        confrimOrder0();
            } else {
                if ("支付宝".equals(payMode)) {
                    getSign();
                } else if ("微信支付".equals(payMode)) {
                    wechatPay();
                } else if ("银联支付".equals(payMode)) {
                    mProgressDialog.dismiss();
                    Intent intent = new Intent();
                    intent.setClass(PayActivity.this, YinLianWebActivity.class);
                    intent.putExtra("BookLogAID", mBookLogAID);
                    intent.putExtra(Constant.IntentKey.PAY_ORDERID, mOrderId);
                    intent.putExtra(Constant.IntentKey.PAY_PRICE, (int) (realPayPrice * 100) + "");
                    if (mRedBag != null) {
                        intent.putExtra(Constant.IntentKey.PAY_REDENVELOPE_ID, mRedBag.getId() + "");
                    }
                    startActivity(intent);
                }
            }
        } else {
            setFailDialog01("支付超时，失败", "确认");
        }
    }

    /**
     * 微信支付
     */
    private void wechatPay() {
        getParams();
        saveWechatPayOutTradeNo(mOutTradeNo, mBookLogAID, mOrderId, realPayPrice, mInsurePrice);
        getXingYeBlankWechatPayOrderPrepayIdAndSign();
    }

    /**
     * 兴业银行-->微信支付
     */
    private void getParams() {
        mOutTradeNo = getOutTradeNo();
        mGetWechatOrderParams = new HashMap<>();
//        mGetWechatOrderParams.put("out_trade_no", mQueryOrder.getAID().substring(0,32));// 商户订单号
        mGetWechatOrderParams.put("body", "车票 " + TimeAndDateFormate.timeFormate(mQueryOrder.getSetoutTime()) + " " + mQueryOrder.getLineName()+" "+ VersionUtils.getCurrVersionInfo(PayActivity.this));// 商品描述
        mGetWechatOrderParams.put("total_fee", TransformYuanToFen(realPayPrice) + "");// 总金额
        mGetWechatOrderParams.put("mch_create_ip", GetIpAddressUtil.getPhoneIp());// 终端IP
        mGetWechatOrderParams.put("id", mOrderId);
        if (mRedBag != null) {
            mGetWechatOrderParams.put("redEnvelope_id", mRedBag.getId() + "");
        }
        mGetWechatOrderParams.put("real_pay", realPayPrice + "");
    }

    /**
     * 将微信支付的商户订单号保存到本地
     */
    private void saveWechatPayOutTradeNo(String outTradeNo, String bookLogAID, String orderID, double realPayPrice, double insurePrice) {
        SharedPreferences sp = getSharedPreferences(Constant.WechatPay.ABOUT_WECHAT_PAY, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(Constant.WechatPay.ABOUT_WECHAT_PAY_OUT_TRADE_NO, outTradeNo);
        edit.putString(Constant.WechatPay.ABOUT_WECHAT_PAY_BOOKLOGAID, bookLogAID);
        edit.putString(Constant.WechatPay.ABOUT_WECHAT_PAY_ORDERID, orderID);
        edit.putString(Constant.WechatPay.ABOUT_WECHAT_PAY_REALPAYPRICE, realPayPrice + "");
        edit.putString(Constant.WechatPay.ABOUT_WECHAT_PAY_INSUREPRICE, insurePrice + "");

        if (mRedBag != null) {
            edit.putInt(Constant.WechatPay.ABOUT_WECHAT_PAY_REDID, mRedBag.getId());
        }
        edit.commit();
    }

    /**
     * 兴业银行-->微信支付:获取服务端的预支付订单及签名…等信息,并向兴业银行-->微信支付调起支付
     */
    private void getXingYeBlankWechatPayOrderPrepayIdAndSign() {
        HTTPUtils.post(PayActivity.this, Constant.WechatPay.GET_WECHAT_ORDER_INFO_URL, mGetWechatOrderParams, new VolleyListener() {
            //        HTTPUtils.post(PayActivity.this, "http://192.168.1.108:8080/app/xy/getprepay", mGetWechatOrderParams, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressDialog.dismiss();
            }

            @Override
            public void onResponse(String s) {
                mProgressDialog.dismiss();
                Log.e(TAG, "onResponse: --向请求签名->>" + s);

                XingYeBlankPayInfo xingYeBlankPayInfo = GsonUtils.parseJSON(s, XingYeBlankPayInfo.class);
                if (xingYeBlankPayInfo != null && xingYeBlankPayInfo.getMap() != null) {
                    if (xingYeBlankPayInfo.getMap().getStatus().equalsIgnoreCase("0")) {
                        isEnterWX = false;
                        RequestMsg msg = new RequestMsg();
                        msg.setMoney((double) TransformYuanToFen(realPayPrice));
                        msg.setTokenId(xingYeBlankPayInfo.getMap().getToken_id());
                        msg.setTradeType(MainApplication.WX_APP_TYPE);
                        msg.setAppId(Constant.WechatPay.APP_ID);
                        PayPlugin.unifiedAppPay(PayActivity.this, msg);
                    }
                } else {
                    showChargeResult("获取签名失败","确认");
                }
            }
        });
    }

    /**
     * 人民币单位转换 元--->>分
     */
    private int TransformYuanToFen(double primevalPrice) {
        int v = (int) (primevalPrice * 100);
        return v;
    }

    /**
     * 两个按钮的dialog
     *
     * @param messageTxt
     */
    private void setDialog(String messageTxt) {
        View doublebuttondialog = getLayoutInflater().inflate(R.layout.doublebuttondialog, null);
        TextView message = (TextView) doublebuttondialog.findViewById(R.id.message);
        message.setText(messageTxt);
        AlertDialog.Builder builder = new AlertDialog.Builder(PayActivity.this);
        final AlertDialog dialog = builder.setView(doublebuttondialog).create();
        dialog.setCancelable(false);
        dialog.show();
        doublebuttondialog.findViewById(R.id.ISee).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                cancleOrder();
            }
        });
        doublebuttondialog.findViewById(R.id.button_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    /**
     * 支付失败，取消订单
     */
    private void cancleOrder() {
        String url = Constant.JDT_TICKET_HOST +
                "SellTicket_NoBill_Cancel?scheduleCompanyCode=" + "YongAn" +
                "&bookLogAID=" + mBookLogAID;
        HTTPUtils.get(PayActivity.this, url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }

            @Override
            public void onResponse(String s) {
                if ("isSure".equals(mIsSure)) {
                    finish();
                } else {
                    startToMainActivity();
                }
                animFromBigToSmallOUT();
            }
        });
    }

    /**
     * 从大到小结束动画
     */
    private void animFromBigToSmallOUT() {
        overridePendingTransition(R.anim.fade_in, R.anim.big_to_small_fade_out);
    }

    //重写back方法
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ("isSure".equals(mIsSure)) {
                finish();
            } else {
                startToMainActivity();
            }
            animFromBigToSmallOUT();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 跳转主页面
     */
    private void startToMainActivity() {
        Intent intent = new Intent();
        intent.putExtra("OrderDeatilActivity", "OrderDeatilActivity");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(PayActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 从左往右打开动画
     */
    private void animFromLeftToRightIN() {
        overridePendingTransition(R.anim.push_right_in, R.anim.fade_out);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isEnterWX) {
            startToMainActivity();
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction("RedBag");
        filter.addAction(PAY_RECEIVER);
        registerReceiver(receiver, filter);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }


    private void showChargeResult(String msg, String comfire) {
        final SingleBtnDialog singleBtnDialog = new SingleBtnDialog(PayActivity.this, msg, comfire);
        singleBtnDialog.setCancelable(false);
        singleBtnDialog.show();
        singleBtnDialog.setClicklistener(new SingleBtnDialog.ClickListenerInterface() {

            @Override
            public void doWhat() {
                singleBtnDialog.dismiss();
            }
        });
    }

    private void showChargeResultSuccess(String msg, String comfire) {
        final SingleBtnDialog singleBtnDialog = new SingleBtnDialog(PayActivity.this, msg, comfire);
        singleBtnDialog.setCancelable(false);
        singleBtnDialog.show();
        singleBtnDialog.setClicklistener(new SingleBtnDialog.ClickListenerInterface() {

            @Override
            public void doWhat() {
                singleBtnDialog.dismiss();
                Intent intent = new Intent();
                intent.putExtra("orderId",mOrderId);
                intent.putExtra("BookLogAID", mBookLogAID);
                intent.putExtra("insurePrice", mInsurePrice);
                intent.putExtra("payMode",payMode);
                intent.setClass(PayActivity.this, OrderDeatilActivity.class);
                startActivity(intent);
                animFromBigToSmallOUT();
            }
        });
    }
}